import RedTarVo from "./RedTarVo";
/**author:yebinx email:yebinx@21cm.com*/
export default class RedVo{
    public type:string;
    private events:Array<RedTarVo>=[];
    constructor(_type:string){
        this.type=_type;
    }

    public add(red:any,_caller:any=null,_methed:Function=null,_param=null){
        if(this.isAdded(red)){
            console.debug("isAdded");
            return;
        }
        this.events.push(new RedTarVo(red,_caller,_methed,_param));
        red.on(Laya.Event.REMOVED,this,this.removeFromStage,[red]);
    }

    public remove(red:any){
		for(let i=this.events.length-1;i>=0;i--){
			if(this.events[i].tar==red){
				let tred:RedTarVo = this.events.splice(i,1)[0];
				tred.destroy();
				tred=null;
			}
		}
    }

    public isAdded(red:any){
        for(let i=this.events.length-1;i>=0;i--){
			if(this.events[i].tar==red){
				return true;
			}
		}
        return false;
    }

    public  updateAll(){
        for(let i=0;i<this.events.length;i++){
            this.events[i].update();
        }
    }

    /**强制设置状态*/
    public  updateSt(_show:boolean){
        for(let i=0;i<this.events.length;i++){
            this.events[i].updateSt(_show);
        }
    }

    private removeFromStage(red:any){
        this.remove(red);
    }
}